package com.mfhcd.facepay.utils;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.text.InputFilter;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;

import com.alibaba.android.arouter.launcher.ARouter;
import com.mfhcd.facepay.constant.SPConstant;

import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.SocketException;
import java.util.Enumeration;

/**
 * Created by Vector
 * on 2017/2/14 0014.
 */
public class Utils {

    public static String getIpAddress(Context context) {
        @SuppressLint("MissingPermission") NetworkInfo info = ((ConnectivityManager) context
                .getSystemService(Context.CONNECTIVITY_SERVICE)).getActiveNetworkInfo();
        if (info != null && info.isConnected()) {
            // 3/4g网络
            if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
                try {
                    for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements(); ) {
                        NetworkInterface intf = en.nextElement();
                        for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                            InetAddress inetAddress = enumIpAddr.nextElement();
                            if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
                                return inetAddress.getHostAddress();
                            }
                        }
                    }
                } catch (SocketException e) {
                    e.printStackTrace();
                }
            } else if (info.getType() == ConnectivityManager.TYPE_WIFI) {
                //  wifi网络
                WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
                WifiInfo wifiInfo = wifiManager.getConnectionInfo();
                String ipAddress = intIP2StringIP(wifiInfo.getIpAddress());
                return ipAddress;
            } else if (info.getType() == ConnectivityManager.TYPE_ETHERNET) {
                // 有限网络
                return getLocalIp();
            }
        }
        return null;
    }

    private static String intIP2StringIP(int ip) {
        return (ip & 0xFF) + "." +
                ((ip >> 8) & 0xFF) + "." +
                ((ip >> 16) & 0xFF) + "." +
                (ip >> 24 & 0xFF);
    }

    // 获取有限网IP
    private static String getLocalIp() {
        try {
            for (Enumeration<NetworkInterface> en = NetworkInterface
                    .getNetworkInterfaces(); en.hasMoreElements(); ) {
                NetworkInterface intf = en.nextElement();
                for (Enumeration<InetAddress> enumIpAddr = intf
                        .getInetAddresses(); enumIpAddr.hasMoreElements(); ) {
                    InetAddress inetAddress = enumIpAddr.nextElement();
                    if (!inetAddress.isLoopbackAddress()
                            && inetAddress instanceof Inet4Address) {
                        return inetAddress.getHostAddress();
                    }
                }
            }
        } catch (SocketException ex) {
        }
        return "0.0.0.0";
    }

    /**
     * 包含中文的字符串长度
     */
    public static int strLen(String str) {
        int valueLength = 0;
        String chinese = "[\u4e00-\u9fa5]";
        for (int i = 0; i < str.length(); i++) {
            String temp = str.substring(i, i + 1);
            if (temp.matches(chinese)) {
                valueLength += 2;
            } else {
                valueLength += 1;
            }
        }
        return valueLength;
    }

    public static String maskIdNo(String idNo) {
        if (TextUtils.isEmpty(idNo))
            return idNo;
        return idNo.substring(0, 6) + "*******" + idNo.substring(idNo.length() - 4);
    }

    public static String maskBankCard(String bankCard) {
        if (TextUtils.isEmpty(bankCard))
            return bankCard;
        return "**** **** **** **** " + bankCard.substring(bankCard.length() - 4);
    }

    public static String maskRealName(String name) {
        if (TextUtils.isEmpty(name))
            return name;
        if (name.length() == 2) {
            return name.substring(0, 1) + "*";
        } else if (name.length() >= 3) {
            StringBuilder mask = new StringBuilder();
            for (int i = 0; i < name.length() - 2; i++)
                mask.append("*");
            return name.substring(0, 1) + mask.toString() + name.substring(name.length() - 1);
        } else {
            return name;
        }
    }

    public static String trimBankCard(String bankCard) {
        if (!TextUtils.isEmpty(bankCard))
            return bankCard.replaceAll(" ", "");
        else
            return bankCard;
    }

    public static String getBankNumSuffix(String bankNum) {
        if (!TextUtils.isEmpty(bankNum) && bankNum.length() >= 4) {
            return bankNum.substring(bankNum.length() - 4);
        } else {
            return bankNum;
        }
    }

    public static String yuanToFen(String yuan) {
        if (TextUtils.isEmpty(yuan)) {
            return "0";
        } else {
            return DecimalUtils.formatFen(DecimalUtils.mul(yuan, "100"));
        }
    }

    public static String fenToYuan(String fen) {
        if (TextUtils.isEmpty(fen)) {
            return "0.00";
        } else {
            return DecimalUtils.div(fen, "100");
        }
    }

    public static boolean checkAmountEmpty(String amount) {
        if (TextUtils.isEmpty(amount)
                || amount.equals("0") || amount.equals("0.") || amount.equals("0.0") || amount.equals("0.00")
                || amount.equals(".") || amount.equals(".0") || amount.equals(".00")) {
            return false;
        } else {
            return true;
        }
    }

    public static boolean checkFeeAmount(String amount, String fee) {
        if (DecimalUtils.compare(amount, fee) <= 0) {
            return false;
        }
        return true;
    }

    public static boolean checkMaxAmount(String amount, String maxAmount) {
        if (!TextUtils.isEmpty(maxAmount) && DecimalUtils.compare(amount, maxAmount) > 0) {
            return false;
        }
        return true;
    }

    /**
     * 隐藏软键盘(只适用于Activity，不适用于Fragment)
     */
    public static void hideSoftKeyboard(Activity activity) {
        View view = activity.getCurrentFocus();
        if (view != null) {
            InputMethodManager inputMethodManager = (InputMethodManager) activity.getSystemService(Activity.INPUT_METHOD_SERVICE);
            inputMethodManager.hideSoftInputFromWindow(view.getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
        }
    }

    /**
     * 转换OCR识别的身份证日期
     */
    public static String parseIDCardDate(String date) {
        if (!TextUtils.isEmpty(date) && date.length() == 8) {
            return date.substring(0, 4) + "-" + date.substring(4, 6) + "-" + date.substring(6);
        } else {
            return date;
        }
    }

    /**
     * @return 中国建设银行 (8888)
     */
    public static String getBankNameWithNumber(String bankName, String bankNum) {
        if (TextUtils.isEmpty(bankName))
            bankName = "";
        if (TextUtils.isEmpty(bankNum))
            bankNum = "";
        else
            bankNum = "  (" + Utils.getBankNumSuffix(bankNum) + ")";
        return bankName + bankNum;
    }

}
